<template>
  <!-- 分配权限的弹层 -->
  <el-dialog title="分配权限" :visible="showDialogAssign" @close="hClose">
    <el-tree
      ref="tree"
      node-key="id"
      :data="permList"
      show-checkbox
      default-expand-all
      check-strictly
      :props="{label: 'name'}"
    />
    <el-button type="primary" size="mini" @click="submit">保存</el-button>
  </el-dialog>
</template>

<script>
import { assignPermission, getPermission } from '@/api/permission'
import { newArr } from '@/utils'
import { getRoleInfo } from '@/api/role'
export default {
  name: 'AssignPermission',
  props: {
    showDialogAssign: {
      type: Boolean,
      default: false
    },
    currentId: {
      type: Number,
      required: true
    }
  },
  data() {
    return {
      permList: []
    }
  },
  created() {
    this.getPermiss()
  },
  methods: {
    hClose() {
      this.$emit('update:showDialogAssign', false)
    },
    async getPermissionList() {
      const res = await getRoleInfo(this.currentId)
      this.$refs.tree.setCheckedKeys(res.data.permIds)
    },
    async getPermiss() {
      const res = await getPermission()
      this.permList = newArr(res.data, 0)
    },
    async submit() {
      const keys = this.$refs.tree.getCheckedKeys()
      console.log(keys)
      const res = await assignPermission({ id: this.currentId, permIds: keys })
      console.log(res)
      this.hClose()
    }
  }
}
</script>
